{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3a79ef54-63e6-429f-ba9f-590b736a7f29",
   "metadata": {},
   "source": [
    "# 3.9 创建Variable对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "- 创建初始值为3，类型为int32的Variable对象re1\n",
    "- 通过Python列表创建初始值为[1,2]，类型为int32的Variable对象re2\n",
    "- 通过NumPy数组创建初始值为[1,2]，类型为int32的Variable对象re3\n",
    "- 创建初始值为3.，类型为float32的Variable对象re4\n",
    "- 创建初始值为[1,2]，类型为float64的Variable对象re5\n",
    "- 通过张量创建初始值为[1,2]，类型为int32的Variable对象re6"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8d7baa9c-93a2-42f3-a3c1-231cdb587f2d",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74ad989a-9b82-43e1-b841-e74284cd5936",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "使用tf.Variable构造方法可以创建一个Variable对象。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>\n",
      "<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([1, 2])>\n",
      "<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([1, 2])>\n",
      "<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=3.0>\n",
      "<tf.Variable 'Variable:0' shape=(2,) dtype=float64, numpy=array([1., 2.])>\n",
      "<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([1, 2])>\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "# 创建初始值为3，类型为int32的Variable对象re1\n",
    "re1=tf.Variable(initial_value=3)\n",
    "print(re1)\n",
    "# 通过Python列表创建初始值为[1,2]，类型为int32的Variable对象re2\n",
    "re2=tf.Variable(initial_value=[1,2])\n",
    "print(re2)\n",
    "# 通过NumPy数组创建初始值为[1,2]，类型为int32的Variable对象re3\n",
    "re3=tf.Variable(initial_value=np.array([1,2]))\n",
    "print(re3)\n",
    "# 创建初始值为3.，类型为float32的Variable对象re4\n",
    "re4=tf.Variable(initial_value=3.)\n",
    "print(re4)\n",
    "# 创建初始值为[1,2]，类型为float64的Variable对象re5\n",
    "re5=tf.Variable(initial_value=[1,2],dtype=tf.float64)\n",
    "print(re5)\n",
    "# 通过张量创建初始值为[1,2]，类型为int32的Variable对象re6\n",
    "re6=tf.Variable(initial_value=tf.constant([1,2]))\n",
    "print(re6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f2c61db-4b69-49f9-9b22-f6d2642a056d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
